<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('新建登记部门')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body>
<div class="main-content">
    <form id="form-depRegister-add" class="form-horizontal">
        <input name="regby" type="hidden" id="regby"/>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>部门代码：</label>
                    <div class="col-sm-8">
                        <input name="depCode" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" id="depCode"
                               class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>部门名称：</label>
                    <div class="col-sm-8">
                        <input name="title" id="title" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>部门简称：</label>
                    <div class="col-sm-8">
                        <input name="depAbbr" id="depAbbr" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>部门级别：</label>
                    <div class="col-sm-8">
                        <select name="depGrade" id="depGrade" class="form-control m-b"
                                th:with="type=${@dict.getType('oCD_DepGrade')}" required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>部门类型：</label>
                    <div class="col-sm-8">
                        <select name="depType" id="depType" class="form-control m-b"
                                th:with="type=${@dict.getType('oCD_DepType')}" required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select></div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>所属公司：</label>
                    <div class="col-sm-8">
                        <select id="compId" name="compId" class="form-control" th:onchange="getbumen(this.value)">
                            <option value=""></option>
                            <option th:each="companys:${companys}" th:value="${companys.compID }"
                                    th:text="${companys.compAbbr }"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">行政上级部门：</label>
                    <div class="col-sm-8">
                        <select name="adminId" id="adminId" class="form-control m-b">
                            <option value="0"></option>
                        </select></div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">职能上级部门：</label>
                    <div class="col-sm-8">
                        <input name="funcationName" placeholder="输入部门代码或名称可自动补全" id="funcationName" class="form-control"
                               type="text">

                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>部门负责人：</label>
                    <div class="col-sm-8">
                        <input name="directorName" placeholder="输入编号或姓名可自动补全" id="directorName" class="form-control"
                               type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>部门分管领导：</label>
                    <div class="col-sm-8">
                        <input name="director2Name" id="director2Name" placeholder="输入编号或姓名可自动补全" class="form-control"
                               type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>成本中心：</label>
                    <div class="col-sm-8">
                        <select name="depCost" id="depCost" class="form-control m-b"
                                th:with="type=${@dict.getType('oCD_DepCost')}" required>
                            <option value=""></option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>成立日期：</label>
                    <div class="col-sm-8">
                        <input name="effectDate" id="effectDate" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>部门属性：</label>
                    <div class="col-sm-8">
                        <select name="depProperty" id="depProperty" class="form-control m-b"
                                th:with="type=${@dict.getType('oCD_DepProperty')}" required>
                            <option value=""></option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"></span>备注：</label>
                    <div class="col-sm-8">
                        <input name="remark" id="remark" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<th:block th:include="include :: bootstrap-typeahead-js"/>

<script>
    var prefix = ctx + "depart/admin";
    $(function () {
        $("#effectDate").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    });

    function submitHandler() {

        var depGrade = $("#depGrade").val();
        var adminId = $("#adminId").val();

        if (depGrade == 1) {
            if (adminId != 0) {
                $.modal.alertWarning('一级部门不能有行政上级部门！')
                return;
            }
        }

        if (depGrade != 1) {
            if (adminId == 0) {
                $.modal.alertWarning('非一级公司必须选择行政上级部门！')
                return;
            }
        }

        if ($.validate.form()) {
            var data = $("#form-depRegister-add").serializeArray();
            var depCode = $("#depCode").val();//代码
            var depAbbr = $("#depAbbr").val();//简称
            var title = $("#title").val();//名称
            var depGrade = $("#depGrade").val();//级别
            var depType = $("#depType").val();//类型
            var compId = $("#compId").val();//所属公司
            var adminId = $("#adminId").val();//职能上级
            var funcationName = $("#funcationName").val();//职能上级
            var directorName = $("#directorName").val();
            var director2Name = $("#director2Name").val();
            var depCost = $("#depCost").val();
            var effectDate = $("#effectDate").val();
            var depProperty = $("#depProperty").val();
            var remark = $("#remark").val();

            data.push({"id": "depCode", "value": depCode});
            data.push({"id": "depAbbr", "value": depAbbr});
            data.push({"id": "depGrade", "value": depGrade});
            data.push({"id": "depType", "value": depType});
            data.push({"id": "compId", "value": compId});
            data.push({"id": "title", "value": title});
            data.push({"id": "funcationName", "value": funcationName});
            data.push({"id": "adminId", "value": adminId});
            data.push({"id": "directorName", "value": directorName});
            data.push({"id": "director2Name", "value": director2Name});
            data.push({"id": "depCost", "value": depCost});
            data.push({"id": "effectDate", "value": effectDate});
            data.push({"id": "depProperty", "value": depProperty});
            data.push({"id": "remark", "value": remark});

            $.operate.saveTab(prefix + "/add/register", data);
        }
    }

    function getbumen(value) {
        var html = "";
        $.ajax({
            async: false,
            url: ctx + "empentry/findDep",
            data: {
                "compID": value
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#adminId").empty();
                var arry = data;
                html += "<option value='0'></option>";
                for (var i = 0; i < arry.length; i++) {
                    html += "<option value='"
                        + arry[i].depID
                        + "'>"
                        + arry[i].title
                        + "</option>";
                }
                $("#adminId").append(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert("错误,请联系管理员,错误信息:" + textStatus);
            }
        });
    }

    $.get(ctx + "depart/admin/getEmpRow", function (data) {
        console.log(data);
        $("#directorName").typeahead({
            source: data.value
        });
        $("#director2Name").typeahead({
            source: data.value
        });
    }, 'json');

    $.get(ctx + "depart/admin/getDepRow", function (data) {
        console.log(data);
        $("#funcationName").typeahead({
            source: data.value
        });
    }, 'json');
</script>
</body>
</html>